﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Game.Contract;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 牌桌界面右下角及时消息-获得好友列表(等同30076)
	/// </summary>
	public class Action30069 : BaseAction
	{
		private string friendsIdStr;
		private List<SimpleUserDataWithTableIds> _friendsTableData;
		private string _friendsTableDataStr;
		public Action30069(HttpGet actionGetter)
			: base((short)ActionType.DZGetRealMessageFriendsTableInfo, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetString("FriendsIdStr", ref friendsIdStr))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var friendsIds = ProtoBufUtils.Deserialize<List<int>>(Convert.FromBase64String(friendsIdStr));
			if (friendsIds.Count <= 0)
			{
				Tool.WriteErrorLine("消息30069获得的好友列表数量是0");
				return false;
			}
			_friendsTableData = new List<SimpleUserDataWithTableIds>();
			foreach (var id in friendsIds)
			{
				var user = GameTable.Current.GetUser(id);
				if (user == null)
					continue;

				_friendsTableData.Add(new SimpleUserDataWithTableIds() { UserData = new SimpleUserData() { HeadIcon = user.HeadIcon, NickName = user.NickName, UserId = user.UserId }, TableIds = user.Property.TableIdList });
			}

			_friendsTableDataStr = Convert.ToBase64String(ProtoBufUtils.Serialize(_friendsTableData));
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this._friendsTableDataStr);
		}
	}
}
